﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

namespace goMatic
{
    public partial class TEMPERATURE : System.Web.UI.UserControl
    {
        public event EventHandler TempChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public TEMPERATURE()
        {
        }

        v_HMDeviceDetails vDetail;
        public v_HMDeviceDetails _Details
        {
            get { return vDetail; ; }
            set
            {
                if (value != null)
                {
                    vDetail = value;
                    dName.Text = vDetail.Name;
                    lDevice.Text = vDetail.Address;
                    if (vDetail.value != null)
                    {
                        _Temperature = double.Parse(vDetail.value, System.Globalization.CultureInfo.InvariantCulture);
                        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
                        TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);
                        lTime.Text = (value.C__createdAt.Value.LocalDateTime + offset).ToString("dd-MM HH:mm:ss");
                    }
                    else
                    {
                        Image2.ImageUrl = "~/Images/unknown.png";
                    }
                }
                else
                {
                    Image2.ImageUrl = "~/Images/unknown.png";
                }
            }
        }

        v_HMDeviceDetails vTargetTemp;
        public v_HMDeviceDetails _TargetTemp
        {
            get { return vTargetTemp; }
            set
            {
                if (value != null)
                {
                    vTargetTemp = value;
                    tb_TargetTemp.Text = vTargetTemp.value;
                    tb_TargetTemp.Visible = true;
                    tb_TargetTemp_SliderExtender.Enabled = true;
                }
            }
        }

        internal double iTemp;
        internal int iHum;

        public double _Temperature
        {
            get { return iTemp; }
            set
            {
                iTemp = value;
                lTemp.Text = iTemp.ToString("N1") + "°";
            }
        }

        public int _Humidity
        {
            get { return iHum; }
            set
            {
                iHum = value;
                lHumidty.Text = (iHum).ToString("N0") + "%";
            }
        }

        public bool ReadOnly { get; set; }

        protected void tb_TargetTemp_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tb_TargetTemp.Text))
            {
                //Only when value changed
                double tOld = Math.Round(double.Parse(vTargetTemp.value, System.Globalization.CultureInfo.InvariantCulture), 1);
                double tNew = Math.Round(double.Parse(tb_TargetTemp.Text, System.Globalization.CultureInfo.InvariantCulture), 1);
                if (tOld != tNew & !ReadOnly)
                {
                    vTargetTemp.value = tb_TargetTemp.Text;

                    CCU.ccucmdSoapClient oCMD = new CCU.ccucmdSoapClient();
                    oCMD.SetValueAsync("", vDetail.Interface, vDetail.Address.Replace(":1", ":2"), "SETPOINT", "double", tb_TargetTemp.Text, vDetail.CustomerID);


                    if (this.TempChanged != null)
                    {
                        this.TempChanged(this, e);
                    }
                }

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dbEntities oDB = new dbEntities();
            DateTime dStartTime = DateTime.Now.Subtract(new TimeSpan(2, 0, 0, 0));
            Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "dd. HH:mm";
            List<ChannelValues> oTVals = oDB.ChannelValues.Where(t => t.customerid == vDetail.CustomerID & t.device == vDetail.Address & t.valuekey == "TEMPERATURE" & t.C__createdAt > dStartTime).ToList();
            DateTime[] xT = oTVals.Select(t => t.C__createdAt.DateTime).ToArray();
            double[] yT = oTVals.Select(t => double.Parse(t.value, System.Globalization.CultureInfo.InvariantCulture)).ToArray();
            List<ChannelValues> oHVals = oDB.ChannelValues.Where(t => t.customerid == vDetail.CustomerID & t.device == vDetail.Address & t.valuekey == "HUMIDITY" & t.C__createdAt > dStartTime).ToList();
            DateTime[] xH = oHVals.Select(t => t.C__createdAt.DateTime).ToArray();
            int[] yH = oHVals.Select(t => int.Parse(t.value)).ToArray();
            //double[] yValues = { 10, 27.5, 7, 12, 45.5 };
            //string[] xNames = { "Mike", "John", "William", "George", "Alex" };
            Chart1.Series[0].Points.DataBindXY(xT, yT);
            Chart1.Series[1].Points.DataBindXY(xH, yH);
            Panel1.Visible = true;
            modpop1.Show();
        }

        protected void btClose_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

    }
}